# Customise this file, documentation can be found here:
# https://github.com/fastlane/fastlane/tree/master/fastlane/docs
# All available actions: https://docs.fastlane.tools/actions
# can also be listed using the `fastlane actions` command

# Change the syntax highlighting to Ruby
# All lines starting with a # are ignored when running `fastlane`

# If you want to automatically update fastlane if a new version is available:
# update_fastlane

# This is the minimum version number required.
# Update this, if you use features of a newer version
fastlane_version "2.32.1"

default_platform :ios

# ------------- CONFIGURATION -------------------------------------------------

PROJECT          = 'ChainReactConf'
BUNDLE_ID        = "com.chainreactapp"
XCODE_PROJECT    = "#{PROJECT}.xcodeproj"
XCODE_WORKSPACE  = "#{PROJECT}.xcworkspace"
XCODE_SCHEME     = PROJECT
BUILD_NUMBER     = get_build_number(xcodeproj: "./#{XCODE_PROJECT}")
VERSION          = get_version_number(xcodeproj: "./#{XCODE_PROJECT}", target: PROJECT)

def fancy_sign(message, size=60, separator='=-')
  puts '\|||/'.center(size, ' ')
  puts '(o o)'.center(size, ' ')
  puts '-ooO-(_)-Ooo-'.center(size, '-')
  puts ''.center(size, separator)
  puts "~  #{message}  ~".center(size, separator)
  puts ''.center(size, separator)
  puts ''.center(size, '-')
end

platform :ios do

  before_all do
    setup_circle_ci
    # load variables from .env file in the root if it exists
    if File.exist?('../.env')
      open('../.env', 'r').readlines.each do |l|
        kv = l.split('=')
        ENV[kv[0]] = kv[1].chomp if kv[1]
      end
    end

    # cocoapods
  end

  desc "Playground for fastlane plugins"
  lane :play do
    fancy_sign number_of_commits

    increment_build_number(xcodeproj: "./#{XCODE_PROJECT}")

    commit_version_bump(
      xcodeproj: "./#{XCODE_PROJECT}",
      ignore: /tvOS/,
      force: true,
      message: "[skip ci] Version bump"
    )
  end

  desc "Increment the build number (like 25)"
  desc "Note: This is NOT the same as the version number (like 2.1.0)"
  lane :bump_build_number do
    increment_build_number(xcodeproj: "./#{XCODE_PROJECT}")
  end

  desc "Submit a new Beta Build to Apple TestFlight"
  desc "This will also make sure the profile is up to date"
  lane :beta do

    increment_build_number(xcodeproj: "./#{XCODE_PROJECT}")

    match(type: "appstore")
    build_ios_app(
      scheme: PROJECT,
      workspace: "./ChainReactConf.xcworkspace",
      xcargs: "-UseNewBuildSystem=NO -allowProvisioningUpdates",
      export_method: "app-store"
    )

    # Ship it!
    upload_to_testflight(
      skip_waiting_for_build_processing: true
    )

    commit_version_bump(
      xcodeproj: "./#{XCODE_PROJECT}",
      ignore: /tvOS/,
      force: true,
      message: "[skip ci] Version bump"
    )
  end

  desc "Deploy a new version to the App Store"
  lane :release do
    # match(type: "appstore")
    # snapshot
    gym(scheme: "ChainReactApp") # Build your app - more options available
    deliver(force: true)
    # frameit
  end

  # You can define as many lanes as you want

  after_all do |lane|
    # This block is called, only if the executed lane was successful

    # slack(
    #   message: "Successfully deployed new App Update."
    # )
  end

  error do |lane, exception|
    # slack(
    #   message: exception.message,
    #   success: false
    # )
  end
end


# More information about multiple platforms in fastlane: https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Platforms.md
# All available actions: https://docs.fastlane.tools/actions

# fastlane reports which actions are used. No personal data is recorded.
# Learn more at https://github.com/fastlane/fastlane#metrics
